<?php 
namespace App\Service;

use App\Model\BotsModel;
use App\Jobs\LoginRobotJob;
use Illuminate\Support\Facades\Cache;
use log;

class BotsService extends BaseService
{

	const BOT_KEY = 'bot_list_key_';
	const STEAMPOWERED_BOT_KEY = 'steampowered_bot_list_key_';

	public static $instance = null;

	public static function getInstance()
	{
		if (self::$instance == null) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	/**
	 * 获取机器人配置
	 * @param  array $info 机器人信息
	 * @return array
	 */
	public function getBotsSetting(array $info)
	{
		$setting = array(
			'bot_id' => $info['bot_id'],
			'username' => $info['steam_account'],
			'password' => $info['password'],
			'apiKey' => $info['apikey'],
			'mobileAuth' => [
			'sharedSecret' => $info['shared_secret'],
			'identitySecret' => $info['identity_secret'],
			'deviceId' => "android:" . preg_replace('/^([0-9a-f]{8})([0-9a-f]{4})([0-9a-f]{4})([0-9a-f]{4})([0-9a-f]{12}).*$/', '$1-$2-$3-$4-$5', md5($info['steam_id'])),
			]
		);

		return $setting;
	}

    /**
     * 获取检测交易链接的机器人
     * @return bool
     */
    public function getCheckTradeUrlLivingBot()
    {
        //获取当前机器的机器人
        $condition['client_no'] = getenv('APP_NO');
        if(empty($condition['client_no'])){
            return false;
        }
        $condition['login_status'] = BotsModel::$loginStatusMap['login'];
        $condition['is_active'] = BotsModel::$activeMap['active'];
        $canFromBotList = BotsModel::getLivingBotsByWhere($condition);
        if (empty($canFromBotList)) {
            return false;
        }
        $canFromBotList = $canFromBotList->toArray();
        shuffle($canFromBotList);
        $rand = array_rand($canFromBotList);
        $botLiving = $canFromBotList[$rand];

        $botLiving['steam'] = $this->getLgoinObjByBotId($botLiving['bot_id'], $botLiving['steam_account']);
        return $botLiving;
    }


	public function setRobotCache($steamAccount, $steamObject, $expire = 86400)
	{
		return Cache::store('redis')->put(self::BOT_KEY.$steamAccount, serialize($steamObject), $expire);
	}

	public function getRobotCache($steamAccount)
	{
		return unserialize(Cache::store('redis')->get(self::BOT_KEY.$steamAccount));
	}

    public function setSteampoweredRobotCache($steamAccount, $steamObject, $expire = 86400)
    {
        return Cache::store('redis')->put(self::STEAMPOWERED_BOT_KEY . $steamAccount, serialize($steamObject), $expire);
    }

    public function getSteampoweredRobotCache($steamAccount)
    {
        return unserialize(Cache::store('redis')->get(self::STEAMPOWERED_BOT_KEY . $steamAccount));
    }

	/**
	 * 获取机器人对象
	 * @param  int $botId 机器人ID
	 * @param  string $account 机器人账号
	 * @return object
	 */
	public function getLgoinObjByBotId($botId, $account, $reload = false)
	{
		$info = BotsModel::getBotsInfoBybotId($botId);
		if (!$info) {
			return false;
		}

		$steam = BotsService::getInstance()->getRobotCache($account);
		if (empty($steam) || $reload) {

			dispatch((new LoginRobotJob($botId))->onQueue(($info['client_no'] ? 'LoginRobotJob_'.$info['client_no'] : 'LoginRobotJob'))->delay(1));
			return false;
		}
		return $steam;
	}
}